package com.guotd.jspwork2.controller;
import com.guotd.jspwork2.utils.JDBCUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.*;
import static com.guotd.jspwork2.utils.JDBCUtil.getConnection;

@WebServlet("/delKind")
public class delKindServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String kindName = request.getParameter("kind");
        try {
            Connection con = getConnection();
            PreparedStatement pstmt = con.prepareStatement("delete from kind where kind=?");
            pstmt.setString(1, kindName);
            request.setAttribute("kind_msg", "删除成功");
            int row = pstmt.executeUpdate();
            if (row > 0) {
                request.getSession().setAttribute("kind_msg", "删除成功");
                request.getRequestDispatcher("getAllKinds").forward(request, response);
            } else {
                request.getSession().setAttribute("kind_msg", "删除失败，分类不存在！");
                request.getRequestDispatcher("getAllKinds").forward(request, response);
            }
        } catch (SQLException e) {
            request.getSession().setAttribute("kind_msg", "删除失败，该分类中还有商品！");
            request.getRequestDispatcher("getAllKinds").forward(request, response);
        }
        /*String sql="delete from kind where kind=?";
        String []args={kindName};
        int row= JDBCUtil.update(sql, args);
        if (row > 0) {
            request.getSession().setAttribute("kind_msg", "删除成功");
            request.getRequestDispatcher("getAllKinds").forward(request, response);
        } else {
            request.getSession().setAttribute("kind_msg", "删除失败，分类不存在！");
            request.getRequestDispatcher("getAllKinds").forward(request, response);
        }*/
    }
}